package icontext

/*

#cgo CFLAGS: -I../../../src_c -I../../../src_c/web_svc/
#define _GNU_SOURCE
#include <pthread.h>
#include <sys/fcntl.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "appconfig.h"
#include "web_server.h"
#include "rrd.h"
#include "rrd2json.h"
#include "log.h"

pthread_t web_server_thread;

void start_web_server() {
	signal(SIGPIPE, SIG_IGN);
    pthread_create (&web_server_thread, NULL, socket_listen_main,
		(void *) 0);
	pthread_setname_np (web_server_thread, "web_svc");
}

*/
import "C"

import (
	"nd-app/pkg/factory"
	"os"
	"unsafe"
)

type WebDataAttr struct {
	Family         *C.char
	Type           *C.char
	Units          *C.char
	Priority       C.long
	UpdateInterval C.int
	Multiplier     C.long
	Divisor        C.long
	ChartType      C.int
	Algorithm      C.int
	IsEnabled      C.int
}

const (
	WEB_DATA_TYPE_SYS_CPU = iota
	WEB_DATA_TYPE_SYS_MEM
	WEB_DATA_TYPE_RADIO_FREQUENCY
	WEB_DATA_TYPE_RADIO_THROUGHOUT
	WEB_DATA_TYPE_RADIO_SIGNAL
	WEB_DATA_TYPE_RADIO_MESSAGE
)

var webDataAttr = []WebDataAttr{
	{C.CString("System"), C.CString("cpu"), C.CString("percentage"), 20, 1, 1, 1, C.RRDSET_TYPE_LINE,
		C.RRDDIM_PCENT_OVER_DIFF_TOTAL, 0},
	{C.CString("System"), C.CString("memory"), C.CString("GB"), 21, 1, 1, 1024 * 1024, C.RRDSET_TYPE_LINE,
		C.RRDDIM_ABSOLUTE, 0},
	{C.CString("Radio"), C.CString("frequency"), C.CString("Hz"), 30, 1, 1, 1, C.RRDSET_TYPE_LINE,
		C.RRDDIM_ABSOLUTE, 1},
	{C.CString("Radio"), C.CString("throughout"), C.CString("MB"), 31, 1, 1, 1024 * 1024, C.RRDSET_TYPE_LINE,
		C.RRDDIM_INCREMENTAL, 1},
	{C.CString("Radio"), C.CString("signal"), C.CString("dbm"), 32, 1, 1, 1, C.RRDSET_TYPE_LINE,
		C.RRDDIM_ABSOLUTE, 1},
	{C.CString("Radio"), C.CString("message"), C.CString("packets/s"), 33, 1, 1, 1, C.RRDSET_TYPE_LINE,
		C.RRDDIM_INCREMENTAL, 1},
}

func InitWeb() error {
	C.listen_port = 49999
	C.rrd_default_history_entries = (7 * 24 * 3600)
	C.access_log_syslog = 0
	C.error_log_syslog = 0
	C.output_log_syslog = 0

	myhostname, err := os.Hostname()
	if err == nil {
		C.hostname = C.CString(myhostname)
	}
	C.hostname = C.CString("VHF L频段干扰仪")

	webDir := C.CString(factory.AppConfig.Configuration.WebDir)
	defer C.free(unsafe.Pointer(webDir))
	section := C.CString("global")
	defer C.free(unsafe.Pointer(section))
	name := C.CString("web files directory")
	defer C.free(unsafe.Pointer(name))
	C.config_set_default(section, name, webDir)

	initWebChart()
	C.start_web_server()

	return nil
}

func initWebChart() {
	for i := 0; i < len(webDataAttr); i++ {
		dataAttr := &webDataAttr[i]
		if dataAttr.IsEnabled == 0 {
			continue
		}
		C.rrdset_create(dataAttr.Family, dataAttr.Type, nil,
			dataAttr.Type, dataAttr.Type, dataAttr.Family,
			dataAttr.Units, dataAttr.Priority,
			dataAttr.UpdateInterval, dataAttr.ChartType)
	}
}

func RrddimFindOrAdd(st *C.RRDSET, id *C.char, multiplier C.long, divisor C.long, algorithm C.int) *C.RRDDIM {
	rd := C.rrddim_find(st, id)
	if rd == nil {
		rd = C.rrddim_add(st, id, nil, multiplier, divisor, algorithm)
	}
	return rd
}

func RrddimFindAndSet(st *C.RRDSET, id string, dataAttr *WebDataAttr, value C.longlong) {
	idCStr := C.CString(id)
	defer C.free(unsafe.Pointer(idCStr))
	rd := RrddimFindOrAdd(st, idCStr, dataAttr.Multiplier, dataAttr.Divisor, dataAttr.Algorithm)
	if rd != nil {
		C.rrddim_set_by_pointer(st, rd, value)
	}
}
